<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysLoginLogDetailVo } from '@/api/system/log/SysLoginLog/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysAccountNameListApi } from '@/api/system/account/SysAccount'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysLoginLogDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const resultOptions = ref<Array<KeyLabelValueVo>>([])
const getResult = async () => {
  const res = await sysDictChooseListApi('sysLoginResult')
  resultOptions.value = res.data
}
getResult()
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysAccountNameList = ref<Array<KeyLabelValueVo>>([])
const getSysAccountNameList = async () => {
  const res = await selectSysAccountNameListApi({})
  sysAccountNameList.value = res.data
}
getSysAccountNameList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'accountId',
    label: '账号',
    component: 'Select',
    componentProps: {
      placeholder: '请选择账号id',
      clearable: true,
      filterable: true,
      options: sysAccountNameList
    }
  },
  {
    field: 'loginTime',
    label: '登录时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择登录时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'result',
    label: '登录结果',
    component: 'Select',
    componentProps: {
      placeholder: '请选择登录结果',
      clearable: true,
      filterable: true,
      options: resultOptions
    }
  },
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'failReason',
    label: '登录失败原因',
    component: 'Input',
    componentProps: {
      placeholder: '请输入登录失败原因',
      clearable: true
    }
  },
  {
    field: 'ipAddress',
    label: '登录IP地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入登录IP地址',
      clearable: true
    }
  }
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  accountId: [required()],
  loginTime: [required()],
  result: [required()],
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('平台类型不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  failReason: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 200) {
          callback(new Error('登录失败原因不能超过200个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  ipAddress: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 60) {
          callback(new Error('登录IP地址不能超过60个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

// 所有内容全部都是不可编辑
schema.forEach((item) => {
  item.componentProps.disabled = true
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
